/**
 * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.opsli.modulars.shop;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.opsli.api.base.result.ResultWrapper;

import org.opsli.api.web.shop.device.SysShopDeviceRestApi;
import org.opsli.api.web.shop.userorder.SysUserOrderRestApi;
import org.opsli.api.web.system.org.SysOrgRestApi;

import org.opsli.api.wrapper.shop.device.SysShopDeviceModel;
import org.opsli.api.wrapper.shop.userorder.SysUserOrderModel;
import org.opsli.common.exception.ServiceException;
import org.opsli.core.utils.DYCUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 店铺配置 Controller
 *
 * @author 17607080935
 * @date 2024-10-12 17:57:15
 */
@Api(tags = "门店小程序控制器")
@Slf4j
@RestController
@RequestMapping("/weMini/NotToken/order")
public class WeChatShopOrderRestController{

    @Autowired
    SysOrgRestApi sysOrgRestApi;

    @Resource
    SysShopDeviceRestApi sysShopDeviceRestApi ;


    @Resource
    SysUserOrderRestApi sysUserOrderRestApi ;

    /**
     * 根据id获取订单信息
     * @return ResultWrapper
     */
    @GetMapping("/getById")
    @ApiOperation(value = "根据id获取订单信息", notes = "根据id获取订单信息 - ID")
    public ResultWrapper<?> getById(String id,String lat,String lon) {
        SysUserOrderModel model = new SysUserOrderModel();
        model.setId(id);
        model.setIzApi(true);
        model = sysUserOrderRestApi.get(model).getData();

        if (model == null){
            throw new ServiceException(500,"订单超时支付已关闭");
        }

        //组装设备信息
        SysShopDeviceModel sysShopDeviceModel = new   SysShopDeviceModel();
        sysShopDeviceModel.setId(model.getDeviceId());
        sysShopDeviceModel.setIzApi(true);
        model.setSysShopDeviceModel(sysShopDeviceRestApi.get(sysShopDeviceModel).getData());

        //组装门店信息
        DYCUtils.getOrgIdByOrgIds(model.getOrgIds());
        model.setSysOrgModel(sysOrgRestApi.getInfoAndDistance(DYCUtils.getOrgIdByOrgIds(model.getOrgIds()),lat,lon).getData());
        return ResultWrapper.getSuccessResultWrapper(model);
    }
}